package dateFitting;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;


public class DataRead {
    private static List<PartData> trainingSet = new ArrayList<>(); // 训练集数据
    private static List<PartData> testSet = new ArrayList<>();     // 测试集数据
    private static boolean isInitialized = false;                 // 初始化标志



    public static void initialize(String filePath, int trainingSize, int testSize,int sheetNo) {
        if (isInitialized) {
            return; // 已初始化则直接返回
        }

        try (FileInputStream fis = new FileInputStream(filePath);
             Workbook workbook = new XSSFWorkbook(fis)) {

            // 从第一个工作表提取数据
            Sheet sheet = workbook.getSheetAt(sheetNo);
            List<PartData> allData = extractData(sheet,trainingSize + testSize);

            // 使用固定随机种子打乱数据（保证可重复性）
            Collections.shuffle(allData, new Random(42));

            // 划分训练集和测试集
            trainingSet = allData.subList(0, trainingSize);
            testSet = allData.subList(trainingSize, trainingSize + testSize);

            isInitialized = true;

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("数据处理器初始化失败: " + e.getMessage());
        }
    }


    public static List<PartData> getTrainingSet() {
        if (!isInitialized) {
            throw new IllegalStateException("数据处理器未初始化，请先调用initialize()方法");
        }
        return Collections.unmodifiableList(trainingSet);
    }


    public static List<PartData> getTestSet() {
        if (!isInitialized) {
            throw new IllegalStateException("数据处理器未初始化，请先调用initialize()方法");
        }
        return Collections.unmodifiableList(testSet);
    }


    private static List<PartData> extractData(Sheet sheet,int size) {
        List<PartData> dataList = new ArrayList<>();
        for (int i = 1; i <= size; i++) { // 前70行数据（跳过表头）
            Row row = sheet.getRow(i);
            if (row == null) continue;

            // 提取四列数据：C(2), D(3), G(6), J(9)
            double time1 = getNumericValue(row.getCell(2)); // 1件用时
            double time3 = getNumericValue(row.getCell(3)); // 3件用时
            double time5 = getNumericValue(row.getCell(6)); // 5件用时
            double time10 = getNumericValue(row.getCell(9)); // 10件用时

            dataList.add(new PartData(time1, time3, time5, time10));
        }
        return dataList;
    }


    private static double getNumericValue(Cell cell) {
        if (cell == null) return 0.0;
        return cell.getCellType() == CellType.NUMERIC ? cell.getNumericCellValue() : 0.0;
    }


    public static class PartData {
        private final double time1;   // 1件用时
        private final double time3;   // 3件用时
        private final double time5;   // 5件用时
        private final double time10;  // 10件用时

        public PartData(double time1, double time3, double time5, double time10) {
            this.time1 = time1;
            this.time3 = time3;
            this.time5 = time5;
            this.time10 = time10;
        }

        @Override
        public String toString() {
            return String.format(
                    "1件: %.2fh | 3件: %.2fh | 5件: %.2fh | 10件: %.2fh",
                    time1, time3, time5, time10
            );
        }

        // Getter方法
        public double getTime1() { return time1; }
        public double getTime3() { return time3; }
        public double getTime5() { return time5; }
        public double getTime10() { return time10; }
    }
}